package org.hilo.boot.core.dubbo;

import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;

/**
 * 
 * @author zollty
 * @since 2020年9月28日
 */
public class DubboMetricsTools {
    public static final String CONSUMER_SIDE = "dubbo.consumer.requests";
    public static final String PROVIDER_SIDE = "dubbo.provider.requests";

    static void collect(boolean consumer, Invoker<?> invoker, Invocation inv, String remoteHost,
            long start, String outcome, String errorCode) {

        Tags tags = Tags.of("service", invoker.getInterface().getName() + "." + inv.getMethodName(),
                "remoteHost", remoteHost, "outcome", outcome,
                "status", errorCode == null ? "0" : errorCode);

        DistributionSummary summary = DistributionSummary
                .builder(consumer ? CONSUMER_SIDE : PROVIDER_SIDE).baseUnit("seconds")
                .description("dubbo metrics").tags(tags).register(Metrics.globalRegistry);
        
        summary.record((System.currentTimeMillis() - start) / 1000D);
    }

}
